package dailyTopic;

import common.TreeNode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author zhangmin
 * @create 2021-08-09 11:13
 * 从上到下打印二叉树
 */
public class levelOrder_offer32_1 {
    public int[] levelOrder(TreeNode root) {
        if(root==null)
            return new int[0];
        Queue<TreeNode> queue=new LinkedList<>();
        List<Integer> res=new ArrayList<>();
        queue.add(root);
        while (!queue.isEmpty()){
            TreeNode node=queue.poll();
            res.add(node.val);
            if (node.left!=null)
                queue.add(node.left);
            if (node.right!=null)
                queue.add(node.right);
        }
        int[] res1=new int[res.size()];

        for (int i = 0; i < res.size(); i++) {
            res1[i]=res.get(i);
        }
        return res1;
    }
}
